﻿namespace MefContrib.Models.Provider
{
    using System;
    using System.Linq;
    using System.Reflection;
    using MefContrib.Models.Provider.Validation;

    public static class MemberInfoServices
    {
        /// <summary>
        /// Gets a <see cref="MemberInfo"/> for the specified member.
        /// </summary>
        /// <param name="typeName">A <see cref="String"/> containing the fully-qualified name of the type.</param>
        /// <param name="memberName">A <see cref="String"/> containing the name of the member.</param>
        /// <returns>A <see cref="MemberInfo"/> object.</returns>
        /// <remarks>Passing in <see langword="null"></see> to the <paramref name="memberName"/> parameter will return the <see cref="MemberInfo"/> for the entire type.</remarks>
        public static MemberInfo GetMemberInfo(string typeName, string memberName)
        {
            typeName
                .Require("typeName")
                .NotNull()
                .NotEmpty();

            Type inspectedType =
                Type.GetType(typeName, false, true);

            return (string.IsNullOrEmpty(memberName)) ?
                inspectedType : inspectedType.GetMember(memberName).FirstOrDefault();
        }
    }
}